package com.jiaok.exception;

import com.jiaok.comm.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author jiaok
 * @Date 2023/5/12 14:55
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler(value = Exception.class)
    public CommonResponse handleException(Throwable throwable) {

        log.error("错误异常{}",throwable);

        return CommonResponse.error(BizCodeEnum.UNKNOW_EXCEPTION.getCode(), BizCodeEnum.UNKNOW_EXCEPTION.getMessage());
    }

    @ExceptionHandler(value = InternalAuthenticationServiceException.class)
    public CommonResponse handleNullUserName(Throwable throwable) {

        log.error("登录异常",throwable);

        return CommonResponse.error(500, "登录失败：登录用户名不能为空");
    }

    @ExceptionHandler(value = BadCredentialsException.class)
    public CommonResponse handleLoginError(Throwable throwable) {

        log.error("登录异常",throwable);

        return CommonResponse.error(500, "登录失败：用户名或密码错误");
    }
}
